﻿using HHG.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnLineExam.Web.mag.newHandlers
{
    /// <summary>
    /// userHandler 的摘要说明
    /// </summary>
    public class userHandler : IHttpHandler
    {
        private HttpContext _httpContext;
        private HttpRequest _httpRequest;
        private HttpResponse _httpResponse;

        private readonly Data.OnlineExamEntities  _db;
        private readonly Data.EfRepository<Data.Users> _userRepository;
        public userHandler() {
           

            _db = new Data.OnlineExamEntities();
            _userRepository = new Data.EfRepository<Data.Users>(_db);
        }

        public void ProcessRequest(HttpContext context)
        {
            _httpContext = context;
            _httpRequest = context.Request;
            _httpResponse = context.Response;
            context.Response.ContentType = "application/json";

            var type = context.Request.Params["type"];

            switch (type)
            {
                case "getlistForSelect":
                    GetlistForSelect();
                    break;

                case "down":
                   
                    break;
                default:
                    break;
            }
        }

        private void GetlistForSelect()
        {
          WriteJson(  _userRepository.TableNoTracking.Select(s=>new  { s.UserID,s.UserName,s.Role.RoleName,s.RoleId,}));
        }

        private void WriteJson(object data)
        {
            _httpResponse.Write(data.SerializeObject());
            _httpResponse.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}